﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using DreamCube.Websites.ExtModel;
using DreamCube.Foundation.Basic.Utility;
using DreamCube.Foundation.Serialization;
using DreamCube.Framework.DataAccess.Basic;

namespace DreamCube.Websites.BLL
{
    public static class ModuleHelper
    {
        #region "字段"

        /// <summary>
        /// 缓存所有的模块数据
        /// </summary>
        private static List<Module> modules = null;

        #endregion

        #region "属性"

        /// <summary>
        /// 系统所有的模块列表
        /// </summary>
        public static List<Module> Modules
        {
            get
            {
                if (!MyWebsite.IsLocalhostWeb())
                {
                    if (modules == null)
                    {
                        modules = DBObjectHelper.GetModelList<Module>(WebSession.CurrentSystem.GetDB(), Module.TableName, "", "status=1");
                        if (modules == null) modules = new List<Module>();
                    }
                    return modules;
                }
                else
                {
                    return DBObjectHelper.GetModelList<Module>(WebSession.CurrentSystem.GetDB(), Module.TableName, "", "status=1");
                }
            }
        }

        #endregion

        #region "静态方法"

        /// <summary>
        /// 获取当前登录用户可以访问的模块json数据
        /// </summary>
        /// <returns></returns>
        public static String GetCurUserCanAccessModulesJSON()
        {
            return GetUserCanAccessModulesJSON(WebSession.CurrentUserName);
        }

        /// <summary>
        /// 获取所有用户可以访问的模块的JSON格式文本
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public static String GetUserCanAccessModulesJSON(String userName)
        {
            if (String.IsNullOrEmpty(userName)) return null;
            List<Module> moduleList = GetUserCanAccessModules(userName);
            //选中所有的父节点（并且按orderinde的升序进行排序）
            IEnumerable<Module> parentModules = moduleList.Where<Module>((m) =>
            {
                return m.ParentID == 0;
            }).OrderBy(module => module.OrderIndex);
            //组装成所有父与子的对应关系
            foreach (Module m in parentModules)
            {
                IEnumerable<Module> childModules = moduleList.Where<Module>((child) =>
                {
                    return child.ParentID == m.ID;
                }).OrderBy(module => module.OrderIndex);
                m.ChildModules.Clear();//先清空数据，因为这个模块对象是静态全局共享的
                foreach (Module child in childModules)
                {
                    //if (!m.ChildModules.Contains(child)) 
                    m.ChildModules.Add(child);
                }
            }
            return MyJson.Serialize(parentModules.ToArray());
        }

        /// <summary>
        /// 根据用户名称获取指定用户可以访问的模块集合
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public static List<Module> GetUserCanAccessModules(String userName)
        {
            if (String.IsNullOrEmpty(userName)) return null;
            List<Module> moduleList = new List<Module>();
            for (Int32 i = 0,j = Modules.Count; i < j; i++)
            {
                if (PowerHelper.HasPower(userName, Modules[i].PowerCode))
                    moduleList.Add(Modules[i]);
            }
            return moduleList;
        }

        /// <summary>
        /// 获取当前用户可以访问的模块集合
        /// </summary>
        /// <returns></returns>
        public static List<Module> GetCurUserCanAccessModules()
        {
            return GetUserCanAccessModules(WebSession.CurrentUserName);
        }

        #endregion
    }
}
